var _Node = require('component/node.js');
var _inherit = require('core/inherit.js');
var _global = require('global/global.js');
var _geometry = require('core/glm.js');
var _vec3 = _geometry.vec3f;

var S = require('res/style.js');
var _Constant = require('scenes/game/constant.js');
var _Skins = require('scenes/game/cellcommon.js');

var SUIT_1 = 0;
var SUIT_2 = 0;
var SUIT_3 = 1;
var SUIT_TOP = 2;

// ==========================
// Cell
// ==========================
/**
 * @param count of this cell
 * @constructor
 */
function Cell(count) {
    _Node.call(this);
    this.setSize(_Constant.cellSize);
    this.setAnchor(0.5, 0.5);
    _Constant.cellSize.scale(0.5, this.mDrawCenter = new _vec3());

    // skin
    this.mSkin = _Skins.createSkin(count);
    this.mSkin.setPosition(this.mDrawCenter);

    // text
    this.mText = S.game.cells.count.createText(count);
    this.mText.setAnchor(0.5, 0.5);
    this.mText.setPosition(this.mDrawCenter[0], this.mDrawCenter[1] + 6);

    this.updateCount(count);
}
_inherit(Cell, _Node);
Cell.prototype.setDisable = function (disable) {
    this.mSkin.setGray(disable);
};
Cell.prototype.setFlip = function (degree) {
};
Cell.prototype.updateCount = function (count, istop) {
    this.mCount = count;
    var suit = SUIT_3;
    if (count == 1) {
        suit = SUIT_1;
    } else if (count == 2) {
        suit = SUIT_2;
    }
    if (istop) {
        suit = SUIT_TOP;
    }
    this.mSkin.updateSkin(count);
    this.mText.setText(count + '');
    this.mText.setVisialbeLayer(suit, 1);

    return this;
};
Cell.prototype.recoverFromLostContext = function (context) {
    this.mText.recoverFromLostContext(context);
    this.mSkin.recoverFromLostContext(context);
};
Cell.prototype.draw = function (context) {
    this.updateMatrix();
    context.pushMatrix(this.mMatrix);
    this.mSkin.draw(context);
    this.mText.draw(context);
    context.popMatrix();
};
Cell.prototype.getCount = function () {
    return this.mCount;
};
Cell.prototype.toString = function () {
    return '[' + this.mCount + ']';
};

module.exports = Cell;